home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / electric / electric.el < prev    next >
Encoding:
Text File  |  1995-05-12  |  6.3 KB  |  161 lines

  1. ;;; electric.el --- window maker and Command loop for `electric' modes.
  2. ;; Keywords: extensions
  3.  
  4. ;; Copyright (C) 1985, 1986, 1992 Free Software Foundation, Inc.
  5. ;; Principal author K. Shane Hartman
  6.  
  7. ;; This file is part of XEmacs.
  8.  
  9. ;; XEmacs is free software; you can redistribute it and/or modify it
  10. ;; under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation; either version 2, or (at your option)
  12. ;; any later version.
  13.  
  14. ;; XEmacs is distributed in the hope that it will be useful, but
  15. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17. ;; General Public License for more details.
  18.  
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  21. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  
  23. ;;; Synched up with: FSF 19.28.
  24.  
  25. ;; This loop is the guts for non-standard modes which retain control
  26. ;; until some event occurs.  It is a `do-forever', the only way out is to
  27. ;; throw.  It assumes that you have set up the keymap, window, and
  28. ;; everything else: all it does is read commands and execute them -
  29. ;; providing error messages should one occur (if there is no loop
  30. ;; function - which see).  The required argument is a tag which should
  31. ;; expect a value of nil if the user decides to punt. The
  32. ;; second argument is a prompt string (defaults to "->").  Given third
  33. ;; argument non-nil, it INHIBITS quitting unless the user types C-g at
  34. ;; toplevel.  This is so user can do things like C-u C-g and not get
  35. ;; thrown out.  Fourth argument, if non-nil, should be a function of two
  36. ;; arguments which is called after every command is executed.  The fifth
  37. ;; argument, if provided, is the state variable for the function.  If the
  38. ;; loop-function gets an error, the loop will abort WITHOUT throwing
  39. ;; (moral: use unwind-protect around call to this function for any
  40. ;; critical stuff).  The second argument for the loop function is the
  41. ;; conditions for any error that occurred or nil if none.
  42.  
  43. (defun Electric-command-loop (return-tag
  44.                   &optional prompt inhibit-quit
  45.                     loop-function loop-state)
  46.   (if (not prompt) (setq prompt "->"))
  47.   (let ((err nil) cmd
  48.     (electrified-buffer (current-buffer)))    ; XEmacs - 
  49.     (while t
  50.       (setq cmd (read-key-sequence (if (stringp prompt)
  51.                                        prompt
  52.                                        (funcall prompt))))
  53.       (or prefix-arg (setq last-command this-command))
  54.       (setq last-command-event (aref cmd (1- (length cmd)))
  55.         current-mouse-event
  56.           (and (or (button-press-event-p last-command-event)
  57.                (button-release-event-p last-command-event)
  58.                (menu-event-p last-command-event))
  59.            last-command-event)
  60.         this-command (if (menu-event-p last-command-event)
  61.                  last-command-event
  62.                              (key-binding cmd))
  63.         cmd this-command)
  64.       (if (or (prog1 quit-flag (setq quit-flag nil))
  65.           (eq (event-to-character last-input-event) (quit-char)))
  66.       (progn (setq unread-command-event nil
  67.                prefix-arg nil)
  68.          ;; If it wasn't cancelling a prefix character, then quit.
  69.          (if (or (= (length (this-command-keys)) 1)
  70.              (not inhibit-quit)) ; safety
  71.              (progn (ding nil 'quit) ; XEmacs - 
  72.                 (message "Quit")
  73.                 (throw return-tag nil))
  74.            (setq cmd nil))))
  75.       (setq current-prefix-arg prefix-arg)
  76.       (if cmd
  77.       (condition-case conditions
  78.           (progn (if (eventp cmd)
  79.              (progn
  80.                (let ((b (current-buffer)))
  81.                  (dispatch-event cmd)
  82.                  (if (not (eq b (current-buffer)))
  83.                  (throw return-tag (current-buffer)))))
  84.                (command-execute cmd))
  85.              (setq last-command this-command)
  86.              (if (or (prog1 quit-flag (setq quit-flag nil))
  87.                  (eq (event-to-character last-input-event)
  88.                  (quit-char)))
  89.              (progn (setq unread-command-event nil)
  90.                 (if (not inhibit-quit)
  91.                     (progn (ding nil 'quit)
  92.                        (message "Quit")
  93.                        (throw return-tag nil))
  94.                   (message "Quit inhibited")
  95.                   (ding)))))
  96.         (error (command-error conditions) ; XEmacs
  97.            (sit-for 2)))
  98.     (ding nil 'undefined-key))
  99.             (and (not (eq (current-buffer) electrified-buffer)) ; XEmacs -
  100.        (not (eq (selected-window) (minibuffer-window)))
  101.        (progn (ding nil 'quit)
  102.           (message "Leaving electric command loop %s."
  103.                "because buffer has changed")
  104.           (sit-for 2)
  105.           (throw return-tag nil)))
  106.       (if loop-function (funcall loop-function loop-state err))))
  107.   ;; ####> - huh?  It should be impossible to ever get here...
  108.   (ding nil 'alarm)
  109.   (throw return-tag nil))
  110.  
  111. ;; This function is like pop-to-buffer, sort of. 
  112. ;; The algorithm is
  113. ;; If there is a window displaying buffer
  114. ;;     Select it
  115. ;; Else if there is only one window
  116. ;;     Split it, selecting the window on the bottom with height being
  117. ;;     the lesser of max-height (if non-nil) and the number of lines in
  118. ;;      the buffer to be displayed subject to window-min-height constraint.
  119. ;; Else
  120. ;;     Switch to buffer in the current window.
  121. ;;
  122. ;; Then if max-height is nil, and not all of the lines in the buffer
  123. ;; are displayed, grab the whole screen.
  124. ;;
  125. ;; Returns selected window on buffer positioned at point-min.
  126.  
  127. (defun Electric-pop-up-window (buffer &optional max-height)
  128.   (let* ((win (or (get-buffer-window buffer) (selected-window)))
  129.      (buf (get-buffer buffer))
  130.      (one-window (one-window-p t))
  131.      (pop-up-windows t)
  132.      (target-height)
  133.      (lines))
  134.     (if (not buf)
  135.     (error "Buffer %s does not exist" buffer)
  136.       (save-excursion
  137.     (set-buffer buf)
  138.     (setq lines (count-lines (point-min) (point-max)))
  139.     (setq target-height
  140.           (min (max (if max-height (min max-height (1+ lines)) (1+ lines))
  141.             window-min-height)
  142.            (save-window-excursion
  143.              (delete-other-windows)
  144.              (1- (window-height (selected-window)))))))
  145.       (cond ((and (eq (window-buffer win) buf)) (select-window win))
  146.         (one-window
  147.          (goto-char (window-start win))
  148.          (pop-to-buffer buffer)
  149.          (setq win (selected-window))
  150.          (enlarge-window (- target-height (window-height win))))
  151.         (t
  152.          (switch-to-buffer buf)))
  153.       (if (and (not max-height)
  154.            (> target-height (window-height (selected-window))))
  155.       (progn (goto-char (window-start win))
  156.          (enlarge-window (- target-height (window-height win)))))
  157.       (goto-char (point-min))
  158.       win)))
  159.  
  160. (provide 'electric)                           ; zaaaaaaap
  161.